package lambda;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class filter {
    public static void main(String[] args) {

        User  user1=new User("张三");
        User  user2=new User("张三");
        User  user3=new User("张三2");
        List<User> users = Arrays.asList(user1, user2, user3);
        List<User> collect = users.stream().filter(distinctByKey(User::getName)).collect(Collectors.toList());
        System.out.println(collect);
    }

    private static  <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}
